App({
  onLaunch: function () {
    // 展示本地存储能力

    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录  获取open.id
    // 登录
    wx.login({
      success: function (res) {
        var that = this;
        var code = res.code; //返回code
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxeb4194dbed5fce65&secret=2436d62d210d8825e0ae456dce1f8911&js_code=' + code + '&grant_type=authorization_code',

          data: {},
          method: "GET",
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            var openid = res.data.openid;//返回openid   
            wx.setStorageSync('use', openid)
          }
        })
      },

    })

    // 获取用户信息
    wx.getSetting({
      success: res => {
       
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo;
              this.globalData.avatar = res.userInfo.avatarUrl;
              this.globalData.nickname = res.userInfo.nickName;
              this.globalData.gender = res.userInfo.gender;
              // console.log(res.userInfo.avatarUrl);console.log('在这里');
              // wx.setStorageSync('headmap', res.userInfo.avatarUrl);

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
              // console.log(res.userInfo.avatarUrl); console.log('在这里');
              // console.log(res.userInfo.nickName); console.log('名字');
              wx.setStorageSync('avater', res.userInfo.avatarUrl);
              wx.setStorageSync('nickname', res.userInfo.nickName);
            }
            
          })
        }else{
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              //this.globalData.userInfo = res.userInfo

              this.globalData.avatar = res.userInfo.avatarUrl;
              this.globalData.nickname = res.userInfo.nickName;
              this.globalData.gender = res.userInfo.gender;
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
              // console.log(this)
            }
          })
        }
      }
    })
  },

  globalData: {//全局变量
    openid: wx.getStorageSync('use')
  },


  storeInfo: {
    store: '',
    store_name: '',
  },
  whereId: {
    uid: '',
    shopid: '',
    openid:'',
  },
  userInfo: {
    id: '',
    user_nicename: '',
    avatar: '',
  },
  globalData: {
    userInfo: null
  },


  convertHtmlToText: function convertHtmlToText(inputText) {
    var returnText = "" + inputText;
    returnText = returnText.replace(/<\/div>/ig, '\r\n');
    returnText = returnText.replace(/<\/li>/ig, '\r\n');
    returnText = returnText.replace(/<li>/ig, ' * ');
    returnText = returnText.replace(/<\/ul>/ig, '\r\n');
    //-- remove BR tags and replace them with line break
    returnText = returnText.replace(/<br\s*[\/]?>/gi, "\r\n");

    //-- remove P and A tags but preserve what's inside of them
    returnText = returnText.replace(/<p.*?>/gi, "\r\n");
    returnText = returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

    //-- remove all inside SCRIPT and STYLE tags
    returnText = returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText = returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText = returnText.replace(/<(?:.|\s)*?>/g, "");

    //-- get rid of more than 2 multiple line breaks:
    returnText = returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\r\n\r\n");

    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g, '');

    //-- get rid of html-encoded characters:
    returnText = returnText.replace(/ /gi, " ");
    returnText = returnText.replace(/&/gi, "&");
    returnText = returnText.replace(/"/gi, '"');
    returnText = returnText.replace(/</gi, '<');
    returnText = returnText.replace(/>/gi, '>');

    return returnText;
  },

  //上传图片类
  uploadimg: function (data) {
    var aa;
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'pics',
      formData: {
        id: data.id
      },
      success: function (res) {
        success++;
        aa = res.data;
       xx.push(aa[0]);

       console.log(xx);

      },
      fail: function (res) {
        fail++;
        // console.log('fail:' + i + "fail:" + fail);
      },
      complete: function (res) {
        // console.log(i);
        i++;
        if (i == data.path.length) {  //当图片传完时，停止调用       
          // console.log('执行完毕');
          // console.log('成功：' + success + " 失败：" + fail);
        } else {
          // console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }

      }
    })
  }  
})